package com.awenhui.demo.extension;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * @author yuxf
 * @version 1.0
 * @date 2021/4/7 10:30
 */
@Component
@Slf4j
public class DecryptConfigBeanFactoryPostProcessor implements BeanFactoryPostProcessor, EnvironmentAware, Ordered {

    private ConfigurableEnvironment environment;
    private String decryptPrefix = "ENC[";                      // 解密前缀标志 默认值
    private String decryptSuffix = "]";                         // 解密后缀标志 默认值
    private String decryptKey = "ktwl@123";               // 解密key 默认值

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = (ConfigurableEnvironment)environment;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        MutablePropertySources propertySources = environment.getPropertySources();

        StreamSupport.stream(propertySources.spliterator(), false)
                .filter(ps -> ps instanceof OriginTrackedMapPropertySource)
                .collect(Collectors.toList())
                .forEach(ps -> convertPropertySource((OriginTrackedMapPropertySource) ps));
        log.info("配置信息解密完成");
        }

    private void convertPropertySource(OriginTrackedMapPropertySource ps) {
        Map<String, Object> source = ps.getSource();
        if(source==null)return;
       // setDecryptProperties(source);
        source.forEach((k,v) -> {
            String value = String.valueOf(v);
            if (!value.startsWith(decryptPrefix) || !value.endsWith(decryptSuffix)) {
                return;
            }
            String cipherText = value.replace(decryptPrefix, "").replace(decryptSuffix, "");
            //  String clearText = DecryptUtil.aesDecrypt(cipherText, decryptKey);
            String clearText =cipherText+"--解密";
           // source.put(k, clearText);
        });

    }
    /**
     * 解密相关属性
     * @param ps
     */
    private void convertPropertySource0(PropertySource<LinkedHashMap> ps) {
        LinkedHashMap source = ps.getSource();
        setDecryptProperties(source);
        source.forEach((k,v) -> {
            String value = String.valueOf(v);
            if (!value.startsWith(decryptPrefix) || !value.endsWith(decryptSuffix)) {
                return;
            }
            String cipherText = value.replace(decryptPrefix, "").replace(decryptSuffix, "");
          //  String clearText = DecryptUtil.aesDecrypt(cipherText, decryptKey);
            String clearText =cipherText+"--解密";
            source.put(k, clearText);
        });
    }
    /**
     *  设置解密属性
     * @param source
     */
    private void setDecryptProperties(LinkedHashMap source) {
        decryptPrefix = source.get("decrypt.prefix") == null ? decryptPrefix : String.valueOf(source.get("decrypt.prefix"));
        decryptSuffix = source.get("decrypt.suffix") == null ? decryptSuffix : String.valueOf(source.get("decrypt.suffix"));
        decryptKey = source.get("decrypt.key") == null ? decryptKey : String.valueOf(source.get("decrypt.key"));
    }

    @Override
    public int getOrder() {
        return 2147483500;
    }
}


